<?php
	include 'operations/connection.php';	
	function approve($idGroup,$idEmployee) {		
		$id = 0;
		$query = mysql_query("SELECT listapprove.Order FROM listapprove  WHERE IDEmployee = $idEmployee AND IDGroupApprove = $idGroup");
		while ($row = mysql_fetch_array($query)){
			$id = $row[listapprove.Order];
			mysql_query("UPDATE listapprove SET IDStatus=1,DateApprove=NOW(),Description=''
						 WHERE IDEmployee=$idEmlp
						 AND IDGroupApprove = $idGroup");
		}
		updateDocument($idGroup, id + 1);	
		echo "list-receiver-not-approved.php";
	}
	
	function updateDocument($idGroup, $id) {
		$pending = 0;
		$query = mysql_query("SELECT listapprove.IDStatus FROM listapprove,approvedoc
				 	WHERE listapprove.IDGroupApprove = approvedoc.IDGroupApprove
					AND listapprove.IDGroupApprove = $idGroup");
		while ($row = mysql_fetch_array($query)) {
			if ($row[listapprove.IDStatus] == 4)
				$pending++;
		}
		if ($pending == 0) {
			mysql_query("UPDATE approvedoc SET IDStatus=1 WHERE IDGroupApprove = $idGroup");
			updateDateApprove($idGroup);
		} else {
			mysql_query("UPDATE listapprove SET IDStatus=2 WHERE listapprove.IDGroupApprove=$idGroup
			AND listapprove.Order=$id");
		}				
	}
	
	function updateDateApprove($idGroup) {
		mysql_query("UPDATE approvedoc SET TimeApprove = NOW() WHERE IDGroupApprove=$idGroup");		
	}
	
	function reject($idGroup,$idEmployee){
		mysql_query("UPDATE listapprove SET IDStatus=3,DateApprove=NOW() WHERE IDEmployee = $idEmployee
					AND IDGroupApprove = $idGroup");
		mysql_query("UPDATE approvedoc SET IDStatus=3 WHERE IDGroupApprove = $idGroup");
		updateDateApprove($idGroup);				
		echo 'list-receiver-not-approved.php';
	}
	
	function viewReference($idGroup,$idEmployee){
		mysql_query("update list_reference set id_status = 1 WHERE id_group=$idGroup AND id_employee=$idEmployee");		
		echo "list-receiver-reference-waiting.php";
	}
?>